import { extend, storage, deserialize } from 'utils';

const login = {

    /**
     * 判断是否登录
     * @return promise
     */
    logged (){
        return new Promise((resolve, reject) => {
            try{
                let value = deserialize(wx.getStorageSync('userInfo'));
                if(value){
                    value.code !== 0 ? resolve(value) : reject(value.code);
                }else{
                    storage.set('userInfo', {
                        code: 0,
                        data: '',
                    });
                    reject(0)
                }
            }catch(e){
                console.log('获取本地userInfo错误');
            }
        })
    },

    /**
     * 登录
     * @params {object} data 用户信息
     * @params {number} mode 1默认接口登录，2手机注册登录，3微信登录
     */
    logon(data = {}, mode = 1, cb){
        let result = extend({}, {
            mode: mode,
            code: 1,
            data: data
        });
        storage.set('userInfo', result, (rs)=>{
            cb && cb();
        })
    },
}

module.exports = {
    login: login
}